﻿using Newtonsoft.Json.Linq;
using RPAVIEW.util;
using RPAVIEW.views.EditPage.flows;
using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.util;
using System.Windows;
using System.Windows.Controls;

namespace RPAVIEW.views.HomePages {
    /// <summary>
    /// TriggerInfo.xaml 的交互逻辑
    /// </summary>
    public partial class TriggerInfo:Window {
        public TriggerPage parentPage;
        public TriggerInfo() {
            InitializeComponent();
        }

        private void EnsureTrigger(object sender, RoutedEventArgs e) {
            if(string.IsNullOrEmpty(this.triggerName.Text)) {
                MessageBox.Show("触发器名称不能为空");
                return;
            }
            if(this.app.SelectedItem == null){
                MessageBox.Show("应用不能为空");
                return;
            }
            this.UpdateSource();
            Trigger<TimeTriggerEntry> trigger = this.DataContext as Trigger<TimeTriggerEntry>;
            string originalName = trigger.triggerName;
            string originalProcessName = trigger.processInfo.processName;
            HttpUtil httpUtil = HttpUtil.getInstance(SystemUtil.GetAPPConfig("ControlBaseIp") + SystemUtil.GetAPPConfig("TriggerUri"));
            //由于现在只有时间触发器，所以暂时类型写死为时间触发器，后期有别的类型触发器后再改
            
            if(string.IsNullOrEmpty(trigger.triggerId)) {
                //添加
                HttpResponse<JObject> response = httpUtil.SendPost<JObject>("/addTimeTrigger", trigger);
                if(response.Code == 500) { 
                    MessageBox.Show(response.Msg);
                    return;
                } else { 
                    //添加触发器
                    ScheduleUtil.InsertTrigger(trigger, WindowUtil.GetWindow<MainWindow>());
                    this.Close();
                }
            } else { 
                //更新
                HttpResponse<JObject> response = httpUtil.SendPost<JObject>("/editTimeTrigger", trigger);
                if(response.Code == 500) { 
                    MessageBox.Show(response.Msg);
                    return;
                } else { 
                    //更新触发器
                    ScheduleUtil.UpdateTrigger(trigger, WindowUtil.GetWindow<MainWindow>(), originalName, originalProcessName);
                    this.Close();
                }
            }
            parentPage.UpdateList();
        }

        private void CloseWindow(object sender, RoutedEventArgs e) {
            this.Close();
            parentPage.UpdateList();
        }

        public void UpdateSource() { 
            this.triggerName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            this.isEnable.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();
            //this.isWait.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();
            this.app.GetBindingExpression(ComboBox.SelectedItemProperty).UpdateSource();
            BaseParamViewAction triggerEntry = this.triggerEntryArea.Children[0] as BaseParamViewAction;
            triggerEntry.UpdateSource();
        }
    }
}
